﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Ch.Admin.Bfs.Sms.Sdmx.Utils;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Control
{

    /// <summary>
    /// Custom UI Type editor for defining internationalized strinf.
    /// </summary>
    public class InternationalStringEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(
            ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        /// <summary>
        /// Provide our editor for multiline text
        /// </summary>
        /// <param name="context"> Information about the context</param>
        /// <param name="provider"> Mechanism for rerieving service object</param>
        /// <param name="value"> actual property value </param>
        /// <returns> edited text </returns>
        public override object EditValue(ITypeDescriptorContext context,
            IServiceProvider provider, object value)
        {
            // Attempts to obtain an IWindowsFormsEditorService.
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)
                provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc == null || value == null)
                return value;

            InternationalizedString istr = value as InternationalizedString;
            UcInternationalString ucIstr = new UcInternationalString(istr);
            ucIstr.Dock = DockStyle.Fill;
            ucIstr._wfes = edSvc;
            edSvc.DropDownControl(ucIstr);
            value = ucIstr.MultilangString;

            // new (or same) property value
            return value;
        }

    }
}
